package com.example.addressdemo;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.addressdemo.DB.DBAdapter;

import static com.example.addressdemo.DB.DBAdapter.KEY_HOME;
import static com.example.addressdemo.DB.DBAdapter.KEY_NAME;
import static com.example.addressdemo.DB.DBAdapter.KEY_PHONE;
import static com.example.addressdemo.DB.DBAdapter.KEY_WORK;

public class AddActivity extends AppCompatActivity {
    private DBAdapter dbAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("AddActivity", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ImageButton ok_add = (ImageButton) findViewById(R.id.ok_add);
        ImageButton no_add = (ImageButton) findViewById(R.id.no_add);
        final EditText add_name = (EditText) findViewById(R.id.add_name);
        final EditText add_phone = (EditText) findViewById(R.id.add_phone);
        final EditText add_work = (EditText) findViewById(R.id.add_work);
        final EditText add_home = (EditText) findViewById(R.id.add_home);

        dbAdapter = new DBAdapter(this);

        Bundle bundle = this.getIntent().getExtras();
        final String type = bundle.getString("type");
        if (type.equals("update")) {
            String name = bundle.getString("name");
            String phone = bundle.getString("phone");
            String work = bundle.getString("work");
            String home = bundle.getString("home");

            add_name.setText(name);
            add_phone.setText(phone);
            add_work.setText(work);
            add_home.setText(home);
        }

        ok_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("AddActivity", "onClick ok_add");
                ContentValues newValues = new ContentValues();
                String name = add_name.getText().toString();
                String phone = add_phone.getText().toString();
                String work = add_work.getText().toString();
                String home = add_home.getText().toString();

                newValues.put(KEY_NAME, name);
                newValues.put(KEY_PHONE, phone);
                newValues.put(KEY_WORK, work);
                newValues.put(KEY_HOME, home);
                long result;

                if (type.equals("update")) {
                    result = dbAdapter.update(newValues);
                } else {
                    result = dbAdapter.insert(newValues);
                }
                System.out.println(result);

                if (result != 0) {
                    Log.d("insert/updtae", " successed");
                    Toast.makeText(getApplicationContext(), "Successed", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(AddActivity.this, DetailActivity.class);
                    Bundle bundleSimple = new Bundle();
                    bundleSimple.putString("name", name);
                    bundleSimple.putString("phone", phone);
                    bundleSimple.putString("work", work);
                    bundleSimple.putString("home", home);

                    intent.putExtras(bundleSimple);
                    startActivity(intent);
                } else {
                    Toast.makeText(getApplicationContext(), "Insert Failed", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(AddActivity.this, MainActivity.class);
                    startActivity(intent);
                }
            }

        });

        no_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
